#!/bin/sh

# release version
version=4.0.3

CWD=$(cd $(dirname $0)/; pwd)
cd $CWD
binfile="deploy-agent"

start()
{
    nohup $CWD/$binfile &> logs/${binfile}/deploy-agent.log &

    sleep 0.5

    if [ $(ps aux|grep -v grep|grep -v control|grep "$binfile" -c) -gt 0 ]
    then
        echo "$binfile started"
    else
         echo "cannot start ${binfile}"
        exit 1
    fi

}

stop()
{
    echo -n $"Shutting down deploy-agent: "
    if [ $(ps aux|grep -v grep|grep -v control|grep "$binfile" -c) -eq 0 ]; then
          echo "${$binfile} already stopped"
          return
    fi
    ps aux|grep -v grep|grep -v control|grep "$binfile"|awk '{print $2}'|xargs kill
    sleep 0.5
    if [ $(ps aux|grep -v grep|grep -v control|grep "$binfile" -c) -eq 0 ]; then
            echo "${$binfile} stopped"
            return
    fi

}

restart()
{
    stop
    start
}

status()
{
        if [ $(ps aux|grep -v grep|grep "$binfile" -c) -eq 0 ]; then
                echo "stoped"
        else
                echo "started"
        fi
}

case "$1" in
    start|stop|restart)
        $1
        ;;
    status)
        status
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart}"
        exit 2
        ;;
esac